﻿using BaiTu.Framework.MVCFramework.Descriptors;
using BaiTu.Framework.MVCFramework.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace BaiTu.Framework.MVCFramework.Parsers
{
    static class ParameterParser
    {
        public static object GetValue(HttpContext context, ParameterDescriptor pd)
        {
            object value = null;

            if (pd.SourceLocation == HttpAttributes.SourceLocation.Any)
            {
                value = TypeCastHelper.Cast(context.Request.Params[pd.KeyInSource], pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.Cookies)
            {
                value = TypeCastHelper.Cast(context.Request.Cookies[pd.KeyInSource].Value, pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.From)
            {
                value = TypeCastHelper.Cast(context.Request.Form[pd.KeyInSource], pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.Headers)
            {
                value = TypeCastHelper.Cast(context.Request.Headers[pd.KeyInSource], pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.QueryString)
            {
                value = TypeCastHelper.Cast(context.Request.QueryString[pd.KeyInSource], pd.ParameterType);
            }
            return value;
        }
    }
}
